﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


namespace WebArchivosTelefonica.Jerarquias
{
    /// <summary>
    /// Control de administracion de los clientes (Creación, borrado y edición)
    /// </summary>
    public partial class ControlClientes : System.Web.UI.UserControl
    {
        /// <summary>
        /// Page load del control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinqDataSourceDetallesClientes_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            ((Cliente)(e.NewObject)).Id = Guid.NewGuid();
            FileUpload logo = DetailsViewCliente.FindControl("FileUploadInsertLogo") as FileUpload;
            if (logo.HasFile)
            {
                if (logo.FileName.EndsWith("bmp") || logo.FileName.EndsWith("jpg"))
                    ((Cliente)(e.NewObject)).Logo = new System.Data.Linq.Binary(logo.FileBytes);
            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int seleccion = int.Parse(e.CommandArgument.ToString());
                HiddenFieldIdCliente.Value = GridViewClientes.DataKeys[seleccion].Value.ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinqDataSourceDetallesCliente_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            GridViewClientes.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinqDataSourceDetallesCliente_Deleted(object sender, LinqDataSourceStatusEventArgs e)
        {
            GridViewClientes.SelectedIndex = -1;
            GridViewClientes.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinqDataSourceDetallesCliente_Updated(object sender, LinqDataSourceStatusEventArgs e)
        {
            GridViewClientes.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinqDataSourceDetallesCliente_Updating(object sender, LinqDataSourceUpdateEventArgs e)
        {
            FileUpload logo = DetailsViewCliente.FindControl("FileUploadEditLogo") as FileUpload;
            if (logo.HasFile)
            {
                string nombreArchivo = logo.FileName.ToLower();

                if (nombreArchivo.EndsWith("bmp") || nombreArchivo.EndsWith("jpg"))
                    ((Cliente)(e.NewObject)).Logo = new System.Data.Linq.Binary(logo.FileBytes);
            }
        }



    }
}